package com.lihao.wanandroid.ui.navigation

import android.os.Bundle
import com.google.android.flexbox.FlexboxLayoutManager
import com.lihao.jetpackcore.base.BaseViewModel
import com.lihao.jetpackcore.ext.nav
import com.lihao.jetpackcore.util.ToastUtil
import com.lihao.wanandroid.R
import com.lihao.wanandroid.app.base.BaseFragment
import com.lihao.wanandroid.app.ext.init
import com.lihao.wanandroid.data.model.NavigationResponse
import kotlinx.android.synthetic.main.fragment_nav_list.*

/**
 * 导航列表界面
 */
class NavListFragment : BaseFragment<BaseViewModel>() {

    var naviData: NavigationResponse? = null

    val mAdapter: NaviItemAdapter by lazy {
        NaviItemAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                val bundle = Bundle()
                bundle.putParcelable("articleData", mAdapter.data[position])
                nav().navigate(R.id.action_to_webFragment, bundle)
            }
        }
    }

    override fun layoutId() = R.layout.fragment_nav_list

    override fun initView(savedInstanceState: Bundle?) {
        naviData = arguments?.getParcelable("data")
        recyclerView.init(FlexboxLayoutManager(context).apply {

        }, mAdapter).run {
            isNestedScrollingEnabled = false
        }
        naviData?.let {
            tvNaviName.text = it.name
            mAdapter.setList(it.articles)
        }
    }

    override fun lazyLoadData() {
    }

    companion object {

        fun createInstant(data: NavigationResponse): NavListFragment {
            val fragment = NavListFragment()
            val bundle = Bundle()
            bundle.putParcelable("data", data)
            fragment.arguments = bundle
            return fragment
        }

    }
}